﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Himp.TaskScheduling
{
    /// <summary>
    /// 转码
    /// </summary>
    public class CodeConversionService : TaskSchedulingAppService, ICodeConversionService
    {
        private IRepository<CodeConversion, long> _codeConversionRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ParamConfigRepository"></param>
        public CodeConversionService(IRepository<CodeConversion, long> codeConversionRepository) 
        {
            _codeConversionRepository = codeConversionRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<CodeConversionDto>> GetListAsync(CodeConversionInput input)
        {
            var iQuery = await _codeConversionRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(input.ConversionType != null, i => i.ConversionType == input.ConversionType);
            iQuery = iQuery.WhereIf(input.Begntime != null, i => i.ConversionTime >= input.Begntime.Value.Date);
            iQuery = iQuery.WhereIf(input.Endtime != null, i => i.ConversionTime < input.Endtime.Value.Date.AddDays(1));

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<CodeConversion>, List<CodeConversionDto>>(iQuery.ToList());

                return new PagedResultDto<CodeConversionDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Skip((input.Pi - 1) * input.Ps).Take(input.Ps);
                var itemsDto = ObjectMapper.Map<List<CodeConversion>, List<CodeConversionDto>>(items.ToList());

                return new PagedResultDto<CodeConversionDto>(totalCount, itemsDto);
            }
        }
    }
}
